package utils

import (
	"github.com/golang-jwt/jwt/v4"
	"github.com/zeromicro/x/errors"
)

// jwt解密
func (Utils) GetJwtToken(secretKey string, iat, seconds int64, userId int64, tokenType string) (string, error) {
	claims := make(jwt.MapClaims)
	claims["exp"] = iat + seconds // 过期时间
	claims["iat"] = iat           // 生成时间
	claims["user_id"] = userId    // 用户id
	if tokenType != "access" && tokenType != "refresh" {
		return "", errors.New(400, "令牌类型填错误, 必须是[access, refresh]之一")
	}
	claims["type"] = tokenType // 令牌类型
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(secretKey))
}
